package com.xnck.demo.activitidemo.controller;

import com.xnck.demo.activitidemo.command.*;
import org.activiti.engine.*;
import org.activiti.engine.repository.Model;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/flow")
public class FlowController {

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    IdentityService identityService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    ManagementService managementService;

    /**
     * 启动一个流程
     * @param modelId
     */
    @RequestMapping(value = "start/{modelId}")
    public void start(@PathVariable("modelId") String modelId) {
        Model modelData = repositoryService.getModel(modelId);
        Map<String, Object> map = new HashMap<>();
        map.put("id", "111111");
        //设置办理人、候选人、候选组
        map.put("assigneeUserId", "admin");
        map.put("candidateUsers", "test1,test2");
        map.put("candidateGroups", "group1,group2");
        ProcessInstance processInstance = null;
        try {
            // 用来设置启动流程的人员ID，引擎会自动把用户ID保存到activiti:initiator中
            identityService.setAuthenticatedUserId("admin");

            processInstance = runtimeService.startProcessInstanceByKey(modelData.getKey(), "myTestFlow1", map);
            String processInstanceId = processInstance.getId();
            System.out.println(processInstanceId);
        } finally {
            identityService.setAuthenticatedUserId(null);
        }
    }

    /**
     * 完成一个任务
     * @param taskId
     */
    @RequestMapping(value = "complate/{taskId}")
    public void complate(@PathVariable("taskId") String taskId) {
        taskService.complete(taskId);
    }

    /**
     * 移动节点
     */
    @RequestMapping(value = "move/{taskId}/{toNodeId}")
    public void move(@PathVariable("taskId") String taskId,
                     @PathVariable("toNodeId") String toNodeId) {
        managementService.executeCommand(new MoveSingleCommand(taskId, toNodeId));
    }

    /**
     * 删除节点
     */
    @RequestMapping(value = "delete/{taskId}")
    public void delete(@PathVariable("taskId") String taskId) {
        managementService.executeCommand(new DeleteTaskCommand(taskId));
    }

    /**
     * 移动子节点
     */
    @RequestMapping(value = "moveSubOut/{taskId}/{toNodeId}")
    public void moveSubOut(@PathVariable("taskId") String taskId,
                           @PathVariable("toNodeId") String toNodeId) {
        managementService.executeCommand(new MoveSubOutCommand(taskId, toNodeId));
    }

    /**
     * 移动多实例节点
     */
    @RequestMapping(value = "moveMutiOut/{taskId}/{toNodeId}")
    public void moveMutiOut(@PathVariable("taskId") String taskId,
                            @PathVariable("toNodeId") String toNodeId) {
        managementService.executeCommand(new MoveMutiOutCommand(taskId, toNodeId));
    }

    /**
     * 增加流程执行实例
     * @param nodeId
     * @param proInstId
     * @param assigneeStr 以逗号隔开的字符串
     */
    @RequestMapping(value = "addExecution/{nodeId}/{proInstId}/{assignees}")
    public void addExecution(@PathVariable("nodeId") String nodeId,
                             @PathVariable("proInstId") String proInstId,
                             @PathVariable("assignees") String assigneeStr) {
        String[] assignees = assigneeStr.split(",");
        for (String assignee : assignees) {
            managementService.executeCommand(
                    new AddMultiInstanceExecutionCmd(
                            nodeId, proInstId, Collections.singletonMap("assignee", (Object) assignee))
            );
        }
    }

    /**
     * 删除流程执行实例
     * @param excutionId
     * @param complated 是否完成此流程执行实例
     */
    @RequestMapping(value = "delExecution/{excutionId}/{complated}")
    public void delExecution(@PathVariable("excutionId") String excutionId,
                             @PathVariable("complated") Boolean complated) {
        managementService.executeCommand(new DeleteMultiInstanceExecutionCmd(excutionId, complated));
    }
}
